package com.china08.yxyapi.api.manage.tec.teaching.zujuan.zujuanlist;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

/**
 * 组卷列表---保存用户的操作记录(记录到学段,学科,版本,册别)
 * 
 */
@CrossOrigin
@RestController
public class ZujuanListInfoController extends YxyApiBaseController {

    @Autowired
    private ElasticQuesService elasticQuesService;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/zujuanlist/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws JsonParseException, JsonMappingException, IOException {

        List<QuestionModel> queslist = reqModel.queslist;
        // 所有的题id
        List<String> quesIdList = queslist.stream().map(x -> x.quesId).collect(Collectors.toList());
        // quesIdList.add("86599_02_25");
        // quesIdList.add("121690_02_25");
        // quesIdList.add("39873_02_25");
        // quesIdList.add("20465_02_25");
        // 返回响应题
        List<QuestionRespModel> list1 = new ArrayList<QuestionRespModel>();
        List<QuestionRespModel> list2 = new ArrayList<QuestionRespModel>();
        List<QuestionRespModel> list3 = new ArrayList<QuestionRespModel>();
        List<QuestionRespModel> list4 = new ArrayList<QuestionRespModel>();
        QuestionRespModel model = null;
        Integer xzPoint = 0;
        Integer dxPoint = 0;
        Integer tkPoint = 0;
        Integer jdPoint = 0;
        Set<Integer> knowIntegers = new HashSet<>();
        for (QuestionModel quesModel : queslist) {
            ElasticYxyYxyQuesResource es = elasticQuesService.findByEsQuesId(quesModel.quesId);
            knowIntegers.addAll(quesModel.know);
            String quesType = es.getQuestionType();
            Integer point = 0;
            Optional<QuestionModel> resOptional = queslist.stream()
                    .filter(x -> StringUtils.equals(x.quesId, es.getId())).findFirst();
            if (resOptional != null) {
                QuestionModel questionModel = resOptional.get();
                point = questionModel.point;
            }
            if (StringUtils.equals("1", quesType)) {
                model = new QuestionRespModel();
                model.quesId = es.getId();
                model.quesBody = es.getQuesBody();
                if (point != null) {
                    xzPoint = xzPoint + point;
                }
                model.point = point;
                list1.add(model);
            } else if (StringUtils.equals("8", quesType)) {
                model = new QuestionRespModel();
                model.quesId = es.getId();
                model.quesBody = es.getQuesBody();
                if (point != null) {
                    dxPoint = dxPoint + point;
                }
                model.point = point;
                list3.add(model);
            } else if (StringUtils.equals("2", quesType)) {
                model = new QuestionRespModel();
                model.quesId = es.getId();
                model.quesBody = es.getQuesBody();
                if (point != null) {
                    tkPoint = tkPoint + point;
                }
                model.point = point;
                list2.add(model);
            } else {
                model = new QuestionRespModel();
                model.quesId = es.getId();
                model.quesBody = es.getQuesBody();
                if (point != null) {
                    jdPoint = jdPoint + point;
                }
                model.point = point;
                list4.add(model);
            }
        }
        RespModel respModel = new RespModel();
        respModel.xzPoint = xzPoint;
        respModel.dxPoint = dxPoint;
        respModel.tkPoint = tkPoint;
        respModel.jdPoint = jdPoint;
        respModel.danxuanList = list1;
        respModel.duoxuanList = list3;
        respModel.tiankongList = list2;
        respModel.jiedaList = list4;
        respModel.knowCount = knowIntegers.size();
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 试题请求模型
        public List<QuestionModel> queslist;
    }

    protected static class QuestionModel {
        // 试题id
        public String quesId;
        // 试题涉及的知识点
        public List<Integer> know;
        // 分数
        public Integer point;
    }

    protected static class RespModel extends YxyApiRespModel {

        public Integer xzPoint;
        public Integer dxPoint;
        public Integer tkPoint;
        public Integer jdPoint;
        public Integer knowCount;

        // 单选
        public List<QuestionRespModel> danxuanList;
        // 多选
        public List<QuestionRespModel> duoxuanList;
        // 填空
        public List<QuestionRespModel> tiankongList;
        // 解答
        public List<QuestionRespModel> jiedaList;
    }

    protected static class QuestionRespModel {
        // 题id
        public String quesId;
        // 题干
        public Object quesBody;
        // 分数
        public Integer point;
    }

}
